<?php
session_start();
if(!isset($_SESSION['logged']) or $_SESSION['logged'] != 1){ //说明未登录
    echo "<script>alert('请登录后再访问本页面！');location.href='login.html';</script>";
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>资料修改</title>
    <style>
        h1 {text-align: center}
        .s{margin-right: 20px}
        .require{color: red}
        .input_width{width: 200px}
    </style>
</head>
<body>
<h1>资料修改</h1>
<?php
include_once "conn.php";
$sql = "select * from userinfo where username = '" . $_GET['username'] . "'";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result) == 0){  //说明未找到当前登录的用户
    echo "<script>alert('当前用户不存在');location.href='login.html';</script>";
    exit;
}
//程序能走到这里，说明用户己找到
$info = mysqli_fetch_array($result,MYSQLI_ASSOC);
//$info['sex]
//print_r($info);
//$info['fav']
$fav = explode(",",$info['fav']);  //用逗号将字符串拆分成数组
?>
<form action="postModify.php" method="post" ><!--onsubmit="return check();"-->
    <table width="400" border="1" cellpadding="8" border-color="black" align="center" style="border-collapse: collapse">
        <tr>
            <td align="right">用户名</td>
            <td><input name="username" class="input_width" id="username" placeholder="请输入用户名" readonly value="<?php echo $info['username'] ?>"><span class="require">*</span></td>
        </tr>
        <tr>
            <td align="right">密码</td>
            <td><input name="pw"  class="input_width" type="password" id="pw" placeholder="如果不修改密码，请保持密码为空"><span class="require">*</span></td>
        </tr>
        <tr>
            <td align="right">确认密码</td>
            <td><input name="cpw"  class="input_width" type="password" id="cpw"><span class="require">*</span></td>
        </tr>
        <tr>
            <td align="right">性别</td>
            <td><input name="sex" type="radio" value="男" <?php if($info['sex'] == '男'){ echo 'checked'; } ?>>男<input name="sex" type="radio" value="女" <?php if($info['sex'] == '女'){ ?>checked<?php } ?>>女</td>
        </tr>
        <tr>
            <td align="right">邮箱</td>
            <td><input name="email"  class="input_width" placeholder="请输入信箱" value="<?php echo $info['email'] ?>"></td>
        </tr>
        <tr>
            <td align="right">爱好</td>
            <td><input name="fav[]" type="checkbox" <?php if(in_array("听音乐",$fav)) {?> checked <?php } ?>value="听音乐">听音乐
                <input name="fav[]" type="checkbox" <?php if(in_array("玩游戏",$fav)) {?> checked <?php } ?> value="玩游戏">玩游戏
                <input name="fav[]" type="checkbox" <?php if(in_array("踢足球",$fav)) {?> checked <?php } ?> value="踢足球">踢足球
            </td>
        </tr>
        <tr>
            <td align="right">个人介绍</td>
            <td><textarea name="intro"  class="input_width"><?php echo $info['intro'] ?></textarea></td>
        </tr>
        <tr >
            <td align="center" colspan="2">
                <input type="submit" value="修改" class="s">
                <input type="reset" value="重置"></td>
        </tr>
    </table>
</form>
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
    $(function (){
    })
    function check(){
        //首先判断用户名是否有输入
        var username = $("#username").val().trim();
        //console.log(username);
        //return false;
        if(username == ''){
            alert('用户名必须填写');
            return false;
        }
        //接下来判断密码是否填写
        var pw = $("#pw").val().trim();
        var cpw = $("#cpw").val().trim();
        if(pw == "" || cpw == ""){
            alert('密码和（或）确认密码必须填写!');
            return false;
        }
        else{
            if(cpw != pw){
                alert('密码和确认密码必须相同!');
                return false;
            }
            else{
                if(pw.length <= 4){
                    alert('密码长度不得小于5位!');
                    return false;
                }
            }
        }
    }
</script>
</body>
</html>